<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>首页</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }

        .container {
            width: 80%;
            margin: 20px auto;
            overflow: hidden;
        }

        header {
            background: #333;
            color: #fff;
            padding: 20px;
            text-align: center;
        }

        nav {
            display: flex;
            justify-content: center;
            background: #555;
            padding: 10px 0;
        }

        nav a {
            color: white;
            text-decoration: none;
            padding: 0 15px;
        }

        nav a:hover {
            background: #444;
        }

        .search-box {
            text-align: center;
            margin-bottom: 20px;
        }

        .search-box input[type="text"] {
            padding: 10px;
            width: 70%;
            margin-right: 5px;
        }

        .search-box input[type="button"] {
            padding: 10px 20px;
            background: #333;
            color: #fff;
            border: none;
            cursor: pointer;
        }

        .search-box input[type="button"]:hover {
            background: #444;
        }

        .news-list {
            list-style: none;
            padding: 0;
        }

        .news-list li {
            background: #fff;
            border: 1px solid #ddd;
            margin-bottom: 10px;
            padding: 10px;
        }

        .news-list li:hover {
            background: #f9f9f9;
        }

        .news-title {
            font-weight: bold;
        }

        .news-summary {
            color: #666;
        }

        footer {
            background: #333;
            color: #fff;
            text-align: center;
            padding: 10px 0;
            position: relative;
            bottom: 0;
            width: 100%;
        }
    </style>
</head>

<body>
    <header>
        <h1>欢迎 <span class="role">访客</span> 来到新闻后台管理网站</h1>
    </header>
    <nav class="nav_menu">
        <a href="index.html">首页</a>
        <a href="news.html">新闻管理</a>
        <a href="files.html">文件管理</a>
        <a href="users.html">用户管理</a>
        <a href="log.html">日志查看</a>
        <a href="login.html">登录</a>
        <a href="logout.html">退出登录</a>
    </nav>
    <div class="container">
        <div class="search-box">
            <form>
                <input type="text" class="query" placeholder="搜索新闻...">
                <input type="button" class="search" value="搜索">
            </form>
        </div>
        <h2>新闻列表</h2>
        <ul class="news-list">
            <li>
                <div class="news-title">标题</div>
                <div class="news-summary">摘要...</div>
                <a href="news_detail.html">阅读详情</a>
            </li>
            <!-- 复制上面的 <li> 来创建更多的新闻条目 -->
        </ul>
    </div>
    <footer>
        <p>版权所有 © 2024 新闻网站</p>
    </footer>
</body>
<script src="./js/jquery-3.7.1.min.js"></script>
<script>
    getMenu()
    getNewsList()
    function getNewsList() {
        let search
        if ($(".query").val() && $(".query").val() != "") {
            search = $(".query").val()
        }
        else {
            search = ""
        }
        $.ajax({
            url: "http://www.dlrb.com:8082/getNewList.php",
            type: "GET",
            xhrFields: {
                withCredentials: true
            },
            data: {
                search: search
            },
            dataType: "json",
            success: (resp) => {
                if (resp.code == 200) {
                    let context = ""
                    for(let item of resp.data){
                        context += `
                            <li>
                                <div class="news-title">${item.title}</div>
                                <div class="news-summary">${item.context.substr(0,10)}...</div>
                                <a href="news_detail.html?nid=${item.id}">阅读详情</a>
                            </li>
                        `
                    }
                    $(".news-list").html(context)
                } else {
                    alert(resp.msg)
                }
            },
            error: (resp) => {
                alert(resp.msg)
            }
        })
    }
    $(".search").click(function () {
        getNewsList()
    })
    function getMenu() {
        let role = cookieToJson(document.cookie).role
        $.ajax({
            url: "http://www.dlrb.com:8082/menu.php",
            type: "GET",
            xhrFields: {
                withCredentials: true
            },
            data: {
                role
            },
            dataType: "json",
            success: (resp) => {
                if (resp.code == 200) {
                   let context = ""
                   for (let item of resp.data) {
                       context += `
                           <a href="${item.url}">${item.des}</a>
                       `
                   }
                   $(".nav_menu").html(context)
                } else {
                    alert(resp.msg)
                }
            },
            error: (resp) => {
                alert(resp.msg)
            }
        })
    }


    function cookieToJson(cookieString) {
        // 处理空字符串
        if (!cookieString || typeof cookieString !== 'string') {
            return {};
        }
        // 分割 Cookie 字符串
        const cookies = cookieString.split(';');
        const result = {};
        // 遍历每个 Cookie
        for (const cookie of cookies) {
            // 跳过空字符串
            if (!cookie.trim()) {
                continue;
            }
            // 查找第一个等号位置
            const equalsIndex = cookie.indexOf('=');

            // 如果没有等号，跳过这个 Cookie
            if (equalsIndex === -1) {
                continue;
            }
            // 提取键和值
            const name = cookie.substring(0, equalsIndex).trim();
            let value = cookie.substring(equalsIndex + 1).trim();
            // 处理可能的 URL 编码
            try {
                value = decodeURIComponent(value);
            } catch (e) {
                // 如果解码失败，使用原始值
                console.warn(`Failed to decode cookie value: ${value}`, e);
            }
            // 添加到结果对象
            result[name] = value;
        }
        return result;
    }
</script>

</html>